/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.common.io;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sss.common.DataReceiver;
import org.sss.common.KeyValues;
import org.sss.mapreduce.Configuration;
import org.sss.mapreduce.DB;
import org.sss.mapreduce.GroupID;
import org.sss.mapreduce.SssException;
import org.sss.mapreduce.StorageNode;

public class RawKeyFirstKeyValueReadeer extends AbstractKeyValueReader {
  @SuppressWarnings("unused")
  private static Logger logger = LoggerFactory.getLogger(RawKeyFirstKeyValueReadeer.class);

  public RawKeyFirstKeyValueReadeer(Configuration conf,
                                    StorageNode node,
                                    GroupID source,
                                    int subhash,
                                    DataReceiver<List<KeyValues>> next) throws SssException {
    super(conf, node, source, subhash, next);
  }

  @Override
  protected void doRead(IOThread iot)
    throws SssException, IOException, InterruptedException {
    assert getSource() != null: "source should not be null";
    DB db = iot.getDB(node);
    int limitNBytes = iot.getIOGetLimitNBytes();

    final KeyFormatter keyFormatter = KeyFormatter.get(getSource().getKeyFormat());
    assert keyFormatter == KeyFormatter.userKeyPriorityFormatter: "KeyFormatter is not userKeyPriorityFormatter.";
    boolean binc = true;
    byte[]       prefix   = KeyFormatter.getPrefix(getSource(), subhash);
    final byte[] sentinel = KeyFormatter.getSentinel(getSource(), subhash);
    byte[] key = null;

    KeyValuesQueue kvq = new KeyValuesQueue(next, limitNBytes);
    try {
      while (true) {
        checkCanceled();
        List<byte[]> kvs = isDestructiveRead() ? db.ranged(prefix, sentinel, limitNBytes, binc)
                                               : db.range(prefix, sentinel, limitNBytes, binc);
        final int count = kvs.size();
        binc = false;
        countup(kvs);

        if (count <= 0) {
          break;
        }
        else {
          prefix = kvs.get(count - 2);
          for (int i = 0; i < count; i+= 2) {
            if (key == null || !keyFormatter.rawKeyEquals(kvs.get(i), key)) {
              key = keyFormatter.getUserKey(kvs.get(i));
              kvq.pushKey(key);
            }
            kvq.pushValue(kvs.get(i + 1));
          }
        }
      }
    }
    finally {
      kvq.pushEnd();
    }
  }
}
